<template>
  <div class="box">
    <div class="scroll-view">
      <projectMsg :projectId="project_id" :hrefProject="true"></projectMsg>
      <div class="user-title-box">基础信息</div>
      <el-form :model="qrkssgRuleForm" :rules="qrkssgRules" ref="qrkssgRuleForm" label-position="top">
        <el-form-item label="开工日期" prop="start_date">
          <!-- :pickerOptions="pickerOptionsEnd" -->
          <el-date-picker class="input-box" v-model="qrkssgRuleForm.start_date" type="datetime" clearable
             placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工长" prop="foreman_id">
          <el-select class="input-box" v-model="qrkssgRuleForm.foreman_id" placeholder="请选择" filterable clearable
            @change="qrkssgChangeFunc">
            <el-option v-for="item in foremanList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开工文件 (.jpg,.png,.gif)" prop="start_file">
          <div class="file-list-box">
            <el-upload class="avatar-uploader" accept="image/*" action="#" :show-file-list="false" :auto-upload="false"
              list-type="picture-card" :on-change="(file, fileList) => {
                tjyfkpzChangeFile(file, fileList, 'start_file');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="file-item-box" v-for="(item, index) in qrkssgRuleForm.start_file" :key="index">
              <div class="el-icon-delete-box" @click="tjyfkpzFileRemove(index, 'start_file')">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name || item.file_name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input :rows="4" class="input-box" v-model="qrkssgRuleForm.remark" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="qrkssgSubmit('qrkssgRuleForm')" :disabled="diaLoading"
          :loading="diaLoading">提交</el-button>
        <el-button @click="qrkssgCancel('qrkssgRuleForm')">关闭</el-button>
      </span>
    </div>
  </div>
</template>
<script>
import projectApi from "@/api/projectApi";
import foremanApi from "@/api/foremanApi";
import projectMsg from "@/components/ProjectMsg";

export default {
  name: "qrkssgCom",
  components: {
    projectMsg,
  },
  data() {
    var start_file = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("开工文件"));
      } else {
        callback();
      }
    };
    return {
      diaLoading: false,
      qrkssgRuleForm: {
        start_date: "",
        foreman_id: "",
        foreman_name: "",
        remark: "",
        start_file: [],
      },
      qrkssgRules: {
        start_date: [{ required: true, message: "请选择开工日期", trigger: "change" }],
        foreman_id: [{ required: true, message: "请选择工长", trigger: "change" }],
        remark: "",
        start_file: {
          required: true,
          validator: start_file,
          trigger: "change",
        },
      },
      foremanList: [], //工长列表
      pickerOptionsEnd: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 86400000; // 86400000毫秒=1天
        },
      },
      project_id: "",
    };
  },
  watch: {},
  computed: {},
  mounted() {
    this.getForemanList();
    this.project_id = this.$route.query.id;
    this.$nextTick(() => {
      let qrkssgRuleForm = this.qrkssgRuleForm;
      const now = new Date();
      qrkssgRuleForm.start_date = `${now.getFullYear()}-${this.padZero(now.getMonth()+1)}-${this.padZero(now.getDate())} ${this.padZero(now.getHours())}:${this.padZero(now.getMinutes())}:${this.padZero(now.getSeconds())}`;
      this.qrkssgRuleForm = qrkssgRuleForm;
    })
  },
  methods: {
    padZero(num) {
      return num < 10 ? `0${num}` : num;
    },
    // 开始施工确认
    qrkssgCancel(formName) {
      this.$goBackTab(this);
    },
    qrkssgSubmit(formName) {
      let qrkssgRuleForm = this.qrkssgRuleForm;
      console.log(qrkssgRuleForm, "开始施工确认参数");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.diaLoading = true;
          let formdata = new FormData();
          formdata.append(
            "data",
            JSON.stringify({
              project_id: this.project_id,
              start_date: qrkssgRuleForm.start_date,
              foreman_id: qrkssgRuleForm.foreman_id,
              foreman_name: qrkssgRuleForm.foreman_name,
              remark: qrkssgRuleForm.remark,
            })
          );
          qrkssgRuleForm.start_file.forEach((item) => {
            formdata.append("start_file[]", item.raw);
          });
          projectApi
            .onSiteStartup(formdata)
            .then((res) => {
              console.log(res, "开始施工确认");
              this.diaLoading = false;
              if (res.code == 200) {
                this.qrkssgCancel();
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getForemanList() {
      foremanApi
        .getForemanList({
          page: 1,
          num: 999,
          show_deactivate: 2,
        })
        .then((res) => {
          console.log(res, "工长列表");
          if (res.code == 200) {
            this.foremanList = res.data.list;
          }
        })
        .catch((err) => { });
    },
    qrkssgChangeFunc(e) {
      let foremanList = this.foremanList;
      let qrkssgRuleForm = this.qrkssgRuleForm;
      let index = foremanList.findIndex((item) => {
        return item.id == e;
      });
      qrkssgRuleForm.foreman_name = foremanList[index].name;
      this.qrkssgRuleForm = qrkssgRuleForm;
    },
    tjyfkpzChangeFile(file, list, type) {
      let qrkssgRuleForm = this.qrkssgRuleForm;
      qrkssgRuleForm.start_file = [file];
      this.qrkssgRuleForm = qrkssgRuleForm;
    },
    tjyfkpzFileRemove(index, type) {
      let qrkssgRuleForm = this.qrkssgRuleForm;
      qrkssgRuleForm.start_file = [];
      this.qrkssgRuleForm = qrkssgRuleForm;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/projectOparet.scss";
@import "@/styles/addPage.scss";
</style>
